/**
 * 
 */
package com.urg.parser;

/**
 * @author rajgoswami
 */
public class ExpressionParser {
	/**
	 * @param expression
	 * @return function
	 */
	public static Function identifyFunction(String expression) {
		expression = expression.trim();
		Function function = Function.valueOf("IF");
		String functionFromExp = expression.substring(0,
				expression.indexOf("("));
		for (Function func : Function.values()) {
			if (func.toString().equalsIgnoreCase(functionFromExp)) {
				function = func;
				break;
			}
		}
		return function;
	}

	/**
	 * @param expression
	 * @return get arguments
	 */
	public static String getFunctionArguments(String expression) {
		String function = identifyFunction(expression).toString();
		System.out.println(function);
		int indexStartBracket = expression.indexOf("(");
		int expressionLength = expression.length();
		int countLeftBrackets = 0;
		int countRightBrackets = 0;
		int indexEndBracket = -1;
		for (int i = indexStartBracket; i < expressionLength; i++) {
			if (String.valueOf(expression.charAt(i)).equalsIgnoreCase("(")) {
				countLeftBrackets++;
			}
			if (String.valueOf(expression.charAt(i)).equalsIgnoreCase(")")) {
				countRightBrackets++;
			}
			if (countLeftBrackets == countRightBrackets
					&& i != indexStartBracket) {
				indexEndBracket = i;
			}
		}
		String arguments = expression.substring(indexStartBracket + 1,
				indexEndBracket).trim();
		arguments = BracketAnalyzer.smartlyTrimEndBrackets(arguments);
		System.out.println(arguments);
		return arguments;
	}

	/**
	 * @param expression
	 * @return
	 */
	public static String getFirstArgument(String expression) {
		Function function = identifyFunction(expression);
		FunctionType functionType = identifyFunctionType(function);
		String arguments = getFunctionArguments(expression);
		String argumentSeparator = ",";
		int startOfArg = 0;
		int endOfArg = arguments.length() - 1;
		if (FunctionType.CONDITIONAL.equals(functionType)) {
			endOfArg = arguments.indexOf(argumentSeparator);
		} else if (FunctionType.AGGREGATION.equals(functionType)) {
			endOfArg = arguments.length() - 1;
		}
		String firstArgument = arguments.substring(startOfArg, endOfArg).trim();
		firstArgument = BracketAnalyzer.smartlyTrimEndBrackets(firstArgument);
		System.out.println(firstArgument);
		return firstArgument;
	}

	/**
	 * @param expression
	 *            expression
	 * @return second argument if present
	 */
	public static String getSecondArgument(String expression) {
		String secondArgument = "";
		Function function = identifyFunction(expression);
		FunctionType functionType = identifyFunctionType(function);
		String arguments = getFunctionArguments(expression);
		String argumentSeparator = ",";
		int startOfArg = 0;
		if (FunctionType.CONDITIONAL.equals(functionType)) {
			startOfArg = arguments.indexOf(argumentSeparator) + 1;
		}
		secondArgument = arguments.substring(startOfArg).trim();
		secondArgument = BracketAnalyzer.smartlyTrimEndBrackets(secondArgument);
		return secondArgument;
	}

	/**
	 * @param function
	 * @return function type
	 */
	public static FunctionType identifyFunctionType(Function function) {
		FunctionType functionType = FunctionType.CONDITIONAL;
		if (function.toString().contains("IF")
				|| function.toString().contains("if")) {
			functionType = FunctionType.CONDITIONAL;
		} else {
			functionType = FunctionType.AGGREGATION;
		}
		return functionType;
	}

	/**
	 * @param expression
	 *            expression
	 * @return expression
	 */
	public Expression parseExpression(String expression) {
		return null;
	}
}
